import { createRouter, createWebHistory } from 'vue-router'
import login from '@/views/login/index.vue'
import reg from '@/views/reg/index.vue'
import main from '@/views/main/index.vue'
import List from '@/views/List/index.vue'
import detail from '@/views/detail/index.vue'

import home from '@/views/main/children/home.vue'
import list from '@/views/main/children/list.vue'
import other from '@/views/main/children/other.vue'
import shop from '@/views/main/children/shop.vue'
import my from '@/views/main/children/my.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      redirect: '/main/home',
    },
    {
      path: '/login',
      name: 'login',
      component: login,
    },
    {
      path: '/detail',
      name: 'detail',
      component: detail,
    },
    {
      path: '/reg',
      name: 'reg',
      component: reg,
    },
    {
      path: '/List',
      name: 'List',
      component: List,
    },
    {
      path: '/main',
      name: 'main',
      component: main,
      children: [
        {
          path: 'home',
          name: 'home',
          component: home,
        },
        {
          path: 'list',
          name: 'list',
          component: list,
        },
        {
          path: 'other',
          name: 'other',
          component: other,
        },
        {
          path: 'shop',
          name: 'shop',
          component: shop,
        },
        {
          path: 'my',
          name: 'my',
          component: my,
        },
      ]
    },
    // {
    //   path: '/about',
    //   name: 'about',
    //   // route level code-splitting
    //   // this generates a separate chunk (About.[hash].js) for this route
    //   // which is lazy-loaded when the route is visited.
    //   component: () => import('../views/AboutView.vue'),
    // },
  ],
})

router.beforeEach((to, from, next) => {
  let isAuthenticated = localStorage.getItem("token")
  if ((to.name == 'my' || to.name == 'shop') && !isAuthenticated) next({ name: 'reg' })
  else next()
})

export default router
